package com.giacom.abc.isabela.view;

import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.inject.Inject;
import javax.inject.Named;

import org.richfaces.event.FileUploadEvent;
import org.richfaces.model.UploadedFile;

@Named
@ManagedBean
@SessionScoped
public class FileUploadBean implements Serializable {
 
	@Inject ItemBean itemBean;
	
	    /**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
	
	private ArrayList<UploadedImage> files = new ArrayList<UploadedImage>();
	
		private int uploadsAvailable = 5;
	    private boolean autoUpload = false;
	    private boolean useFlash = false;

	    public FileUploadBean() {
	    }

	    public void paint(OutputStream stream, Object object) throws IOException {
	        System.out.println("FileUploadBean.paint()");
	    	stream.write(getFiles().get((Integer) object).getData());
	        stream.close();
	    }	    
	    
	    public void listener(FileUploadEvent event) throws Exception{
	    	UploadedFile item = event.getUploadedFile();
	     
	        UploadedImage file = new UploadedImage();
	        file.setLength(item.getData().length);
	        System.out.println("FileUploadBean.listener() - Name:"+item.getName());
	        file.setName(item.getName());
	        file.setData(item.getData());
	        files.add(file);
	        	        
	        System.out.println(event.getSource());
	        
	        itemBean.getItem().setImagem(item.getInputStream());
	        
	        //salva a imagem
	        //new Image().saveImage(item.getInputStream(), cat.getNome());
	        
	    }  
	      
	    public String clearUploadData() {
	        System.out.println("FileUploadBean.clearUploadData()");
	    	files.clear();
	        return null;
	    }
	 
	    public int getSize() {
	        System.out.println("FileUploadBean.getSize()");
	    	if (getFiles().size() > 0) {
	            return getFiles().size();
	        } else {
	            return 0;
	        }
	    }
	    
	
	    public int getUploadsAvailable() {
	        return uploadsAvailable;
	    }

	    public void setUploadsAvailable(int uploadsAvailable) {
	        this.uploadsAvailable = uploadsAvailable;
	    }

	    public boolean isAutoUpload() {
	        return autoUpload;
	    }

	    public void setAutoUpload(boolean autoUpload) {
	        this.autoUpload = autoUpload;
	    }

	    public boolean isUseFlash() {
	        return useFlash;
	    }

	    public void setUseFlash(boolean useFlash) {
	        this.useFlash = useFlash;
	    }

	    public long getTimeStamp() {
	        System.out.println("FileUploadBean.getTimeStamp()");
	    	return System.currentTimeMillis();
	    }
	 
	    public ArrayList<UploadedImage> getFiles() {
	        System.out.println("FileUploadBean.getFiles()");
	    	return files;
	    }
	 
	    public void setFiles(ArrayList<UploadedImage> files) {
	        System.out.println("FileUploadBean.setFiles()");
	    	this.files = files;
	    }
    
}
